package com.meadvc.ssmdemo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meadvc.ssmdemo.dao.LoginDao;
import com.meadvc.ssmdemo.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @program: ssm-demo
 * @description:登录service实现类
 * @author: meadvc
 * @create: 2020-06-15 16:19
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginDao loginDao;


    /**
     * 利用Shiro框架进行安全控制，后期待完善
     * @param jsonObject
     * @return
     */
    @Override
    public JSONObject authLogin(JSONObject jsonObject) {

        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        return authLogin2(username,password);
    }

    @Override
    public JSONObject authLogin2(String username, String password) {
        JSONObject user = loginDao.getUser(username,password);
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        System.out.println(user);
        if(user.getString("username").equals(username)){
            data.put("username",username);
            meta.put("msg","登录成功");
            meta.put("status",200);

        }else{
            meta.put("msg","登录失败");
            meta.put("status",400);
        }
        resultJson.put("data", data);
        resultJson.put("meta", meta);
        return resultJson;
    }

}
